# coding=utf-8
from appium import webdriver
from time import sleep

from colorama import init, Fore

init(autoreset=True)

desired_caps = {
    "platformName": "Android",
    "automationName": "uiautomator2",
    "platformVersion": "9",
    "deviceName": '2025061700001872',
    "appPackage": "com.seven.qaml.burstserven",
    "appActivity": "com.seven.qaml.burstserven.ui.MenuActivity",
    'skipServerInstallation': False,
    'skipDeviceInitialization': False,
    "noReset": True
}
appium_server_url = 'http://127.0.0.1:4723'
driver = webdriver.Remote(appium_server_url, desired_caps)

count = 0
while count < 100:
    sleep(3)
    driver.find_element('id', 'com.seven.qaml.burstserven:id/id_tv_setting').click()
    sleep(1)
    while True:
        if driver.find_element('id', 'com.seven.qaml.burstserven:id/tv_hint_sure').text == "按确定键开始扫描":
            driver.keyevent(66)
            print("等待扫描结果...")
            break
    while True:
        # if driver.find_element('id', 'com.seven.qaml.burstserven:id/tv_scan_num').text == "100" and driver.find_element(
        #         'id', 'com.seven.qaml.burstserven:id/tv_hint').text == "存在雷管未注册，请按【FN】查看未注册的雷管！":
        #     print("扫描完成")
        #     break
        if driver.find_element('id', 'com.seven.qaml.burstserven:id/tv_hint').text == "存在雷管未注册，请按【FN】查看未注册的雷管！":
            print("扫描完成")
            print (f"本次扫描到 {driver.find_element('id', 'com.seven.qaml.burstserven:id/tv_scan_num').text} 个雷管")
            break
    driver.keyevent(4)
    sleep(1)
    driver.keyevent(66)
    count += 1
    print(Fore.GREEN + f"已完成 {count} 次组网检测")
